package com.cmf.controller;

import com.cmf.common.PageResult;
import com.cmf.common.Result;
import com.cmf.dto.EmployeeDto;
import com.cmf.dto.EmployeeLoginDto;
import com.cmf.dto.EmployeeUpdateDto;
import com.cmf.enums.ResultCodeType;
import com.cmf.service.EmployeeService;
import com.cmf.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: cmf
 * @Date: 2025/8/27 16:16
 * @Version: v1.0.0
 * @Description: TODO
 **/
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @PostMapping ("/login")
    public Result<EmployeeVo> login(HttpServletRequest httpServletRequest, @RequestBody @Validated EmployeeLoginDto employeeDto) {
        return employeeService.login(httpServletRequest, employeeDto);
    }

    @PostMapping("/logout")
    public Result<Void> logout(HttpServletRequest httpServletRequest) {
        return employeeService.logout(httpServletRequest);
    }

    @PostMapping
    public Result<Void> addEmployee(@RequestBody @Validated EmployeeDto employeeDto) {
        return employeeService.addEmployee(employeeDto);
    }
    @GetMapping("/page")
    public Result<PageResult<EmployeeVo>> findPage(@RequestParam Long page, @RequestParam Long pageSize,@RequestParam(required = false, defaultValue = "") String name) {
        return employeeService.findPage(page, pageSize, name);
    }

    @GetMapping("/{id}")
    public Result<EmployeeVo> findById(@PathVariable Long id) {
        return employeeService.findById(id);
    }

    @PutMapping
    public Result<Void> updateEmployee(@RequestBody @Validated EmployeeUpdateDto employeeDto) {
        return employeeService.updateEmployee(employeeDto);
    }
}
